#include <am.h>
#include <klib.h>
#include <klib-macros.h>

#define IP_BASE_ADDR 0x10004000
#define REG_MSIP *((volatile uint32_t *)(IP_BASE_ADDR))
#define REG_MTIMEL *((volatile uint32_t *)(IP_BASE_ADDR + 4))
#define REG_MTIMEH *((volatile uint32_t *)(IP_BASE_ADDR + 8))
#define REG_MTIMECMPL *((volatile uint32_t *)(IP_BASE_ADDR + 12))
#define REG_MTIMECMPH *((volatile uint32_t *)(IP_BASE_ADDR + 16))

int main(){
    putstr("clint test\n");
    for(int i = 0; i < 3; i++) {
        printf("i: %d, mtime: %llx\n", i, (((uint64_t) REG_MTIMEH) << 32) | REG_MTIMEL);
    }

    REG_MTIMECMPH = 0;
    REG_MTIMECMPL = (uint32_t) 0x1FFFF;
    printf("mtimecmp: %llx\n", (((uint64_t) REG_MTIMECMPH) << 32) | REG_MTIMECMPL);

    while(1) {
        uint64_t mtime = (((uint64_t) REG_MTIMEH) << 32) | REG_MTIMEL;
        uint64_t mtimecmp = (((uint64_t) REG_MTIMECMPH) << 32) | REG_MTIMECMPL;
        if(mtime >= mtimecmp) break;
    }
    printf("test done\n");

    return 0;
}
